<script setup>
import { ref } from 'vue'
import { useRouter } from 'vue-router'
import { ElMessage } from 'element-plus'
import { registerAPI } from '@/apis/user'

// 表单数据
const form = ref({
  userCode137: '',
  userName137: '',
  userPass137: '',
  userPass137Confirm: '',
  userPhone137: '',
  userEmail137: '',
  agree: false
})

const formRef = ref()
const router = useRouter()
const submitting = ref(false)

// 表单校验规则
const rules = {
  userCode137: [
    { required: true, message: '用户账号不能为空', trigger: 'blur' },
    { min: 4, max: 20, message: '账号长度为4-20位', trigger: 'blur' }
  ],
  userName137: [
    { required: true, message: '用户名不能为空', trigger: 'blur' },
    { min: 2, max: 10, message: '用户名长度为2-10位', trigger: 'blur' }
  ],
  userPass137: [
    { required: true, message: '密码不能为空', trigger: 'blur' },
    { min: 6, max: 14, message: '密码长度为6-14位', trigger: 'blur' }
  ],
  userPass137Confirm: [
    { required: true, message: '确认密码不能为空', trigger: 'blur' },
    { 
      validator: (rule, value, callback) => {
        if (value === '') {
          callback(new Error('请再次输入密码'))
        } else if (value !== form.value.userPass137) {
          callback(new Error('两次输入密码不一致'))
        } else {
          callback()
        }
      },
      trigger: 'blur'
    }
  ],
  userPhone137: [
    { required: true, message: '手机号不能为空', trigger: 'blur' },
    { pattern: /^1[3-9]\d{9}$/, message: '请输入正确的手机号', trigger: 'blur' }
  ],
  userEmail137: [
    { required: true, message: '邮箱不能为空', trigger: 'blur' },
    { type: 'email', message: '请输入正确的邮箱地址', trigger: 'blur' }
  ],
  agree: [
    {
      validator: (rule, value, callback) => {
        if (value) {
          callback()
        } else {
          callback(new Error('请勾选协议'))
        }
      },
      trigger: 'change'
    }
  ]
}

// 提交表单
const submitForm = async () => {
  try {
    if (submitting.value) return
    await formRef.value.validate()
    submitting.value = true
    
    // 准备注册数据（不包含确认密码字段）
    const formData = {
      userCode137: form.value.userCode137,
      userName137: form.value.userName137,
      userPass137: form.value.userPass137,
      userPhone137: form.value.userPhone137,
      userEmail137: form.value.userEmail137
    }
    
    const res = await registerAPI(formData)
    
    if (res.code === 200) {
      ElMessage.success(res.msg || '注册成功')
      // 注册成功后跳转到登录页
      router.push('/login')
    } else {
      ElMessage.warning(res.msg || '注册失败')
    }
  } catch (error) {
    console.error('注册出错:', error)
    ElMessage.error(error.message || '注册过程中出现错误')
  } finally {
    submitting.value = false
  }
}
</script>

<template>
  <div>
    <header class="login-header">
      <div class="container m-top-20">
        <h1 class="logo">
          <RouterLink to="/">理工商城</RouterLink>
        </h1>
        <RouterLink class="entry" to="/">
          进入网站首页
          <i class="iconfont icon-angle-right"></i>
          <i class="iconfont icon-angle-right"></i>
        </RouterLink>
      </div>
    </header>
    
    <section class="login-section">
      <div class="wrapper">
        <nav>
          <a href="javascript:;">账户注册</a>
        </nav>
        <div class="account-box">
          <div class="form">
            <el-form 
              ref="formRef" 
              :model="form" 
              :rules="rules" 
              label-position="right" 
              label-width="80px"
              status-icon
            >
              <el-form-item prop="userCode137" label="用户账号">
                <el-input v-model="form.userCode137" placeholder="请输入用户账号" />
              </el-form-item>
              
              <el-form-item prop="userName137" label="用户名">
                <el-input v-model="form.userName137" placeholder="请输入用户名" />
              </el-form-item>
              
              <el-form-item prop="userPass137" label="密码">
                <el-input 
                  v-model="form.userPass137" 
                  type="password" 
                  placeholder="请输入密码" 
                />
              </el-form-item>
              
              <el-form-item prop="userPass137Confirm" label="确认密码">
                <el-input 
                  v-model="form.userPass137Confirm" 
                  type="password" 
                  placeholder="请再次输入密码" 
                />
              </el-form-item>
              
              <el-form-item prop="userPhone137" label="手机号">
                <el-input v-model="form.userPhone137" placeholder="请输入手机号" />
              </el-form-item>
              
              <el-form-item prop="userEmail137" label="邮箱">
                <el-input v-model="form.userEmail137" placeholder="请输入邮箱" />
              </el-form-item>
              
              <el-form-item prop="agree" label-width="22px">
                <el-checkbox v-model="form.agree" size="large">
                  我已同意隐私条款和服务条款
                </el-checkbox>
              </el-form-item>
              
              <el-button 
                size="large" 
                class="subBtn" 
                @click="submitForm" 
                :loading="submitting"
                :disabled="submitting"
              >
                {{ submitting ? '注册中...' : '立即注册' }}
              </el-button>
            </el-form>
            
            <div style="text-align: center; margin-top: 20px;">
              <RouterLink to="/login" style="color: #069;">
                已有账号？立即登录
              </RouterLink>
            </div>
          </div>
        </div>
      </div>
    </section>

    <footer class="login-footer">
      <div class="container">
        <p>
          <a href="javascript:;">关于我们</a>
          <a href="javascript:;">帮助中心</a>
          <a href="javascript:;">售后服务</a>
          <a href="javascript:;">配送与验收</a>
          <a href="javascript:;">商务合作</a>
          <a href="javascript:;">搜索推荐</a>
          <a href="javascript:;">友情链接</a>
        </p>
        <p>CopyRight &copy; 理工商城</p>
      </div>
    </footer>
  </div>
</template>

<style scoped lang="scss">
.login-header {
  background: #fff;
  border-bottom: 1px solid #e4e4e4;

  .container {
    display: flex;
    align-items: flex-end;
    justify-content: space-between;
  }

  .logo {
    width: 200px;

    a {
      display: block;
      height: 132px;
      width: 100%;
      text-indent: -9999px;
      background: url("@/assets/images/logo.png") no-repeat center 18px / contain;
    }
  }

  .sub {
    flex: 1;
    font-size: 24px;
    font-weight: normal;
    margin-bottom: 38px;
    margin-left: 20px;
    color: #666;
  }

  .entry {
    width: 120px;
    margin-bottom: 38px;
    font-size: 16px;

    i {
      font-size: 14px;
      color: $xtxColor;
      letter-spacing: -5px;
    }
  }
}

.login-section {
  background: url('@/assets/images/login-bg.png') no-repeat center / cover;
  height: 588px;
  position: relative;

  .wrapper {
    width: 380px;
    background: #fff;
    position: absolute;
    left: 50%;
    top: 54px;
    transform: translate3d(100px, 0, 0);
    box-shadow: 0 0 10px rgba(0, 0, 0, 0.15);

    nav {
      font-size: 14px;
      height: 55px;
      margin-bottom: 20px;
      border-bottom: 1px solid #f5f5f5;
      display: flex;
      padding: 0 40px;
      text-align: right;
      align-items: center;

      a {
        flex: 1;
        line-height: 1;
        display: inline-block;
        font-size: 18px;
        position: relative;
        text-align: center;
      }
    }
  }
}

.login-footer {
  padding: 30px 0 50px;
  background: #fff;

  p {
    text-align: center;
    color: #999;
    padding-top: 20px;

    a {
      line-height: 1;
      padding: 0 10px;
      color: #999;
      display: inline-block;

      ~a {
        border-left: 1px solid #ccc;
      }
    }
  }
}

.account-box {
  .toggle {
    padding: 15px 40px;
    text-align: right;

    a {
      color: $xtxColor;

      i {
        font-size: 14px;
      }
    }
  }

  .form {
    padding: 0 20px 20px 20px;

    &-item {
      margin-bottom: 28px;

      .input {
        position: relative;
        height: 36px;

        >i {
          width: 34px;
          height: 34px;
          background: #cfcdcd;
          color: #fff;
          position: absolute;
          left: 1px;
          top: 1px;
          text-align: center;
          line-height: 34px;
          font-size: 18px;
        }

        input {
          padding-left: 44px;
          border: 1px solid #cfcdcd;
          height: 36px;
          line-height: 36px;
          width: 100%;

          &.error {
            border-color: $priceColor;
          }

          &.active,
          &:focus {
            border-color: $xtxColor;
          }
        }

        .code {
          position: absolute;
          right: 1px;
          top: 1px;
          text-align: center;
          line-height: 34px;
          font-size: 14px;
          background: #f5f5f5;
          color: #666;
          width: 90px;
          height: 34px;
          cursor: pointer;
        }
      }

      >.error {
        position: absolute;
        font-size: 12px;
        line-height: 28px;
        color: $priceColor;

        i {
          font-size: 14px;
          margin-right: 2px;
        }
      }
    }

    .agree {
      a {
        color: #069;
      }
    }

    .btn {
      display: block;
      width: 100%;
      height: 40px;
      color: #fff;
      text-align: center;
      line-height: 40px;
      background: $xtxColor;

      &.disabled {
        background: #cfcdcd;
      }
    }
  }

  .action {
    padding: 20px 40px;
    display: flex;
    justify-content: space-between;
    align-items: center;

    .url {
      a {
        color: #999;
        margin-left: 10px;
      }
    }
  }
}

.subBtn {
  background: $xtxColor;
  width: 100%;
  color: #fff;
}
</style>